package com.zzjcy.common.core.init;

import com.zzjcy.common.domain.SystemFixedParam;
import com.zzjcy.common.mapper.user.SystemFixedParamMapper;
import com.zzjcy.common.util.Transformer;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

import java.util.List;

/**
 * Created by zhangzhi on 2017/8/7.
 */
public class InstantiationTracingBeanPostProcessor implements ApplicationListener<ContextRefreshedEvent> {
	private static Logger logger = Logger.getLogger(InstantiationTracingBeanPostProcessor.class);

	@Value("${sys.module}")
	private String sysModule;

	@Autowired
	private SystemFixedParamMapper systemFixedParamMapper;

	@Override
	public void onApplicationEvent(ContextRefreshedEvent event) {
		if(event.getApplicationContext().getParent() == null){
			List<SystemFixedParam> sysList = systemFixedParamMapper.queryByModule(sysModule);
			for(SystemFixedParam temp : sysList){
				String value = Transformer.parseEncValues(temp.getValue()).trim();
				System.setProperty(temp.getKey(),value.trim());
				logger.debug("加载到*****temp.getKey() =" + temp.getKey() + "*****temp.getValue() =" + value.trim() +"***");
			}
		}
	}
}